//
//  QLCustomViewController.m
//  QLOperation
//
//  Created by Shrek on 15/5/26.
//  Copyright (c) 2015年 Personal. All rights reserved.
//

/** QLDEBUG Print | M:method, L:line, C:content*/
#ifdef DEBUG
#define QLLog(FORMAT, ...) fprintf(stderr,"M:%s|L:%d|C->%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define QLLog(FORMAT, ...)
#endif

#import "QLCustomViewController.h"
#import "QLAppModel.h"
#import "QLDownloadOperation.h"

@interface QLCustomViewController () <UITableViewDataSource, UITableViewDelegate, QLDownloadOperationDelegate>
{
    __weak IBOutlet UITableView *_tableView;
}

@property (nonatomic, strong) NSArray *arrAppModels;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@property (nonatomic, strong) NSDictionary *dicOperations; /** key:url, value:operation */
@property (nonatomic, strong) NSDictionary *dicImages; /** key:url, value:image */

@end

@implementation QLCustomViewController

- (NSDictionary *)dicImages {
    if (!_dicImages) {
        _dicImages = [[NSDictionary alloc] init];
    }
    return _dicImages;
}

- (NSDictionary *)dicOperations {
    if (!_dicOperations) {
        _dicOperations = [[NSDictionary alloc] init];
    }
    return _dicOperations;
}

- (NSOperationQueue *)operationQueue {
    if (!_operationQueue) {
        _operationQueue = [[NSOperationQueue alloc] init];
        _operationQueue.maxConcurrentOperationCount = 3; /** 设置最大并发数 */
    }
    return _operationQueue;
}

- (NSArray *)arrAppModels {
    if (!_arrAppModels) {
        NSArray *arrTemps = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
        NSMutableArray *arrMAppModels = [NSMutableArray arrayWithCapacity:arrTemps.count];
        [arrTemps enumerateObjectsUsingBlock:^(NSDictionary *dicData, NSUInteger idx, BOOL *stop) {
            QLAppModel *model = [[QLAppModel alloc] initWithDictionary:dicData];
            [arrMAppModels addObject:model];
        }];
        _arrAppModels = [arrMAppModels copy];
    }
    return _arrAppModels;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrAppModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *strId = @"cellStyle1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strId];
    }
    QLAppModel *model = self.arrAppModels[indexPath.row];
    cell.textLabel.text = model.name;
    
    UIImage *image = self.dicImages[model.icon];
    if (image) {
        cell.imageView.image = image;
    } else {
        cell.imageView.image = [UIImage imageNamed:@"activeBattle"];
        QLDownloadOperation *operation = self.dicOperations[model.icon];
        if (operation) {
            
        } else {
            operation = [[QLDownloadOperation alloc] init];
            operation.url = model.icon;
            operation.indexPath = indexPath;
            operation.delegate = self;
            [self.operationQueue addOperation:operation];
            
            NSMutableDictionary *dicMOperations = [NSMutableDictionary dictionaryWithDictionary:self.dicOperations];
            dicMOperations[model.icon] = operation;
            self.dicOperations = [dicMOperations copy];
        }
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QLLog(@"%s~%@", __FUNCTION__, indexPath);
    
}

#pragma mark - QLDownloadOperationDelegate
- (void)downloadOperation:(QLDownloadOperation *)operation DidFinishDownloadImage:(UIImage *)image {
    QLLog(@"%@", image);
    NSMutableDictionary *dicMOperations = [NSMutableDictionary dictionaryWithDictionary:self.dicOperations];
    [dicMOperations removeObjectForKey:operation.url];
    self.dicOperations = [dicMOperations copy];
    
    NSMutableDictionary *dicMImages = [NSMutableDictionary dictionaryWithDictionary:self.dicImages];
    dicMImages[operation.url] = image;
    self.dicImages = [dicMImages copy];
    
    [_tableView reloadRowsAtIndexPaths:@[operation.indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

@end
